package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import cn.hls.dao.UserRoleDao;
import cn.hls.entity.UserRole;
import cn.hls.entity.vo.PageInfo;
import cn.hls.exception.BussinessException;
import cn.hlsconstant.DataAssessEnum;

/**
 * @fileName: UserRoleDaoImpl.java
 * @package: com.bangni.dao.impl
 * @Description: 用户角色数据操作接口实现类
 * @author: yinghui.zhang
 * @email: uuhui@163.com
 * @date: 2015-5-2
 */
@Component("userRoleDaoImpl")
public class UserRoleDaoImpl extends BaseDaoImpl<UserRole> implements
		UserRoleDao {

	@Override
	public PageInfo<UserRole> loadUserRoleInfo(PageInfo<UserRole> page,
			Long roleId, Long userId) throws Exception {
		//定义查询hql语句
		StringBuilder selectHql = new StringBuilder();
		//定义汇总hql语句
		StringBuilder counthql = new StringBuilder();
		//定义查询条件参数
		List<Object> paramList = new ArrayList<Object>();
		selectHql.append(" from UserRole p inner join fetch p.role r inner join fetch p.user u  ");
		counthql.append("select count(p.id) from UserRole p inner join  p.role r inner join  p.user u ");
		//定义条件查询hql语句
		StringBuilder conditionHql=new StringBuilder("where p.deleted=? ");
		paramList.add(false);
		if (roleId != null && roleId != 0) {
			conditionHql.append(" and r.id = ?  ");
			paramList.add(roleId);
		}
		if (userId != null && userId != 0) {
			conditionHql.append(" and u.id = ?   ");
			paramList.add(userId);
		}
		if(conditionHql.length()>0){
			selectHql.append(conditionHql);
			counthql.append(conditionHql);
		}
		try {
			return find(selectHql.toString(), counthql.toString(), paramList.toArray(),
					page);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BussinessException(DataAssessEnum.SELECT_ERROR,"用户权限信息查询失败");
		}
	}

	@Override
	public List<UserRole> loadUserRoleInfo(Long roleId, Long userId)
			throws Exception {
		//定义查询hql语句
		StringBuffer selectHql = new StringBuffer();
		//定义查询条件参数
		List<Object> paramList = new ArrayList<Object>();
		selectHql.append(" from UserRole where deleted= ");
		paramList.add(false);
		if (roleId != null && roleId != 0) {
			selectHql.append(" and role.id = ?  ");
			paramList.add(roleId);
		}
		if (userId != null && userId != 0) {
			selectHql.append(" and user.id = ?   ");
			paramList.add(userId);
		}
		 try {
			return findByHql(selectHql.toString(), paramList.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			throw new BussinessException(DataAssessEnum.SELECT_ERROR,"您所拥有的权限查询失败");
		}
	}

	@Override
	public void deleteUserRole(Long roleId, Long userId) throws Exception {
		StringBuilder hql = new StringBuilder("DELETE FROM  UserRole ur WHERE  deleted=? ");
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(false);
		if(roleId!=null&& roleId!=0){
			hql.append(" and ur.role.id =?");
			paramList.add(roleId);
		}
		if(userId!=null&& userId!=0){
			hql.append(" and ur.user.id =?");
			paramList.add(userId);
		}		
			try {
				 executeHQLUpdate(hql.toString(),paramList.toArray());
			} catch (Exception e) {
				e.printStackTrace();
				throw new BussinessException(DataAssessEnum.DELETE_ERROR,"您的权限删除失败");

			}
		
	}

}
